# 异步爬取小说，提高效率。
import requests
from lxml import etree
import aiohttp
import asyncio
import aiofiles

async def download_ct(url):
    # 通过异步方式建立会话。
    async with aiohttp.ClientSession() as session:
        async with session.get(url) as resp2:
            source = await resp2.text()     # 获取网页源代码
            tree2 = etree.HTML(source)
            name = tree2.xpath('/html/body/div[2]/div[3]/div[3]/div/div[1]/div[2]/div[2]/text()')
            content = tree2.xpath('/html/body/div[2]/div[3]/div[3]/div/div[1]/div[5]/p/text()')
            s = ''.join(content)    # 将内容从list--》str
            # aiofiles 只能写入str，不能写入list格式
            async with aiofiles.open('../小说/'+str(name),mode='w',encoding='utf-8') as f:
                await f.write(s)
# 创建异步任务
async def Createtasks(content_html):
    tasks = []
    for i in content_html:
        tasks.append(asyncio.create_task(download_ct(i)))
    await asyncio.wait(tasks)

if __name__ == '__main__':
    url = 'http://book.zongheng.com/showchapter/1025395.html'
    resp = requests.get(url)
    tree = etree.HTML(resp.text)
    content_html = tree.xpath('/html/body/div[3]/div[2]/div[2]/div/ul/li/a/@href')
    asyncio.run(Createtasks(content_html))